<template>
  <van-uploader
    v-model="node.answer"
    multiple
    :max-count="node.upload_num_max"
    :after-read="afterread"
  />
</template>

<script>
import { uploadImage } from "@/api/form";
export default {
  props: ["node"],

  data() {
    return {};
  },

  created() {},

  methods: {
    upload(file, r) {
      let spark = new SparkMD5.ArrayBuffer();
      let fileReader = new FileReader();
      fileReader.readAsArrayBuffer(file.slice(0, file.size));
      fileReader.onload = (e) => {
        spark.append(e.target.result);
        let md5 = spark.end();
        let form = new FormData();
        form.set("file", file);
        form.set("md5", md5);
        uploadImage(form).then((e) => {
          r.url = e.file;
          r.id=e.id
          r.status = "done";
          delete r.content;
          delete r.file;
          delete r.message;
          delete r.status;
        });
      };
    },
    afterread(r) {
      if (!r.length) {
        r.message = "上传中.....";
        r.status = "uploading";
        let file = r.file;
        this.upload(file, r);
      } else {
        for (let i = 0; i < this.node.answer.length; i++) {
          this.node.answer[i].message = "上传中.....";
          this.node.answer[i].status = "uploading";
          let file = this.node.answer[i].file;
          this.upload(file, this.node.answer[i]);
        }
      }
    },
  },
};
</script>

<style scoped>
</style>